<template xmlns:v-bind="http://www.w3.org/1999/xhtml">
  <div>
    <ddnav></ddnav>
    <div class="dd-body"></div>
    <div class="detail-body" :zDepth="3">
      <mu-row gutter>
        <mu-col width="100" tablet="100" desktop="100" :zDepth="3">
          <mu-card style="width: 100%;margin:0 auto;" :zDepth="3">
            <mu-card-media>
              <iframe id="play_frame" @click="playItem(playUrl, 0)" frameborder="0" style="height: 400px;width: 100%;" src=""></iframe>
            </mu-card-media>
            <mu-card-text>
              <div>
                片名：{{detail.title}}<br/>
                评分：<span style="color: red">{{detail.score}}</span><br/>
                更新：{{detail.updateText}}<br>
                播放地址：<br>
              </div>
              <div v-for="item in detail.videoFroms" :key="item.id">
                <div>{{item.fromSiteName}}</div>
                <div class="demo-paper" v-for="(play, index) in item.players" :key="play.id">
                  <mu-raised-button @click="playItem(play.url, index)" :label="play.name" v-if="activePlayId == index" secondary />
                  <mu-raised-button @click="playItem(play.url, index)" :label="play.name" v-if="activePlayId != index"  />
                </div>
              </div>
            </mu-card-text>
          </mu-card>
        </mu-col>
      </mu-row>
    </div>
  </div>
</template>

<script>
  import {configs} from '../assets/js/config.js'
  import ddnav from './header/Header'
  export default {
    name: 'detail',
    data () {
      return {
        activePlayId: 0,
        detail: {
          title: '',
          updateText: '',
          picUrl: '',
          score: 0.0,
          players: []
        },
        playUrl: ''
      }
    },
    computed: {
      frameUrl: function () {
        return 'http://' + configs.domain + '/api/player/html/?requestUrl=' + encodeURIComponent(this.playUrl)
      }
    },
    methods: {
      playItem: function (url, playId) {
        this.playUrl = url
        var playFrame = document.getElementById('play_frame')
        playFrame.src = url
        this.activePlayId = playId
      },
      postIframe: function () {
        const _this = this
        const url = _this.playUrl
        var playFrame = document.getElementById('play_frame')
        playFrame.src = url

//        const html = '<form action="' + url + '" method="post" target="_self" id="postData_form"></form>'
//        let playIframe = document.getElementById('play_frame')
//        playIframe.contentWindow.document.write(html)
//        playIframe.contentWindow.document.getElementById('postData_form').submit()
      }
    },
    mounted () {
      const _this = this
      _this.$nextTick(function () {
        const detailUrl = 'http://' + configs.domain + '/api/video/' + this.$route.params.id
        _this.$http.jsonp(detailUrl)
          .then(function (response) {
            _this.detail = response.body
            _this.playUrl = response.body.videoFroms[0].players[0].url
            _this.postIframe()
          })
          .catch(function (response) {
            console.log(response)
          })
      })
    },
    components: {ddnav}
  }
</script>

<style lang="css">
  .detail-body {
    margin: 10px;
    padding: 10px;
  }

  .detail-body .cell {
    margin-top: 5px;
  }

  .demo-paper {
    display: inline-block;
    height: auto;
    width: auto;
    margin: 5px;
    padding: 5px;
    text-align: center;
    cursor: pointer;
  }
</style>
